from collections import OrderedDict

from django.conf import settings
from fdfs_client.client import Fdfs_client
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回数据
    """
    return {
        'token': 'Bearer ' + token,
        'status': 0,
        'message': "登录成功！"
    }


# 自定义分页器
class PageNum(PageNumberPagination):
    page_size = 2
    page_query_param = 'pagenum'
    page_size_query_param = 'pagesize'

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            ('status', 0),
            ('message', '获取文章列表成功'),
            ('data', data)
        ]))


def upload_img(img):
    """
    自定义图片上传至fdfs
    :param img: 前端传来的图片blob二进制
    :return: 文件索引
    """
    value = img.file.getvalue()
    client = Fdfs_client(settings.FASTDFS_PATH)
    ret = client.upload_by_buffer(value)
    if ret['Status'] == 'Upload successed.':
        file_id = ret['Remote file_id']
    else:
        file_id = ""
    return file_id






















